package com.itboye.zhujia.login;

import java.util.Observable;
import java.util.Observer;

import org.apache.http.HttpRequest;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.zhujia.R;
import com.itboye.zhujia.base.BaseActivity;
import com.itboye.zhujia.bean.PersonDataBean;
import com.itboye.zhujia.bean.RegisterBean;
import com.itboye.zhujia.bean.ResultEntity;
import com.itboye.zhujia.constants.SPContants;
import com.itboye.zhujia.controller.LoginController;
import com.itboye.zhujia.controller.LoginedState;
import com.itboye.zhujia.me.ActivitySetting;
import com.itboye.zhujia.net.CodeErrorException;
import com.itboye.zhujia.net.XErrorListener;
import com.itboye.zhujia.net.XRequestListener;
import com.itboye.zhujia.presenter.UserPresenter;
import com.itboye.zhujia.util.ByAlert;
import com.itboye.zhujia.util.DebugLog;
import com.itboye.zhujia.util.SPUtils;
import com.itboye.zhujia.util.T;
import com.umeng.socialize.utils.Log;
/**
 * 注册第3步（设置密码）
 *
 */
public class RegisterActivityThree extends BaseActivity implements OnClickListener,Observer{
	private ImageView close_icon;//返回图片
	private TextView add_shap_title_tv;//标题
	
	private EditText res_three_num;//密码
	private TextView res_three_ok;//完成并登陆
	private Intent intent;
	
	private String phone;//接上页面传来的手机号
	private String yzm;//接上页面传来的验证码
	
	UserPresenter userPresenter;
	@Override
	public int initView() {
		return 0;
	}

	@Override
	public void onMyCreate() {
		setContentView(R.layout.activity_register_three);
		
		add_shap_title_tv.setText("设置密码");
		
		intent = super.getIntent();
		phone = intent.getStringExtra("phone");
		yzm = intent.getStringExtra("yzm");

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			
			break;
		case R.id.res_three_ok:
			UserRegister();
			break;

		default:
			break;
		}
		
	}
	/**
	 * 注册
	 */
	private void UserRegister(){
		
		if(TextUtils.isEmpty(res_three_num.getText().toString())){
			ByAlert.alert("请输入密码");
			return;
		}
		userPresenter=new UserPresenter(this);
		userPresenter.register("3", "0", yzm, phone, res_three_num.getText().toString());
		
	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity=handlerError(data);
		if(entity!=null){
			if(!entity.getCode().equals(0)){
				if(entity.getEventType()==UserPresenter.register_success){
					RegisterBean bean=(RegisterBean) entity.getData();
					Log.i("result", bean.toString());
					if(bean!=null){
				//		SPUtils.put(RegisterActivityThree.this, null, SPContants.USER_ID, response);
					//	SPUtils.put(RegisterActivityThree.this, null, SPContants.USERNAME,phone);
								
					//	SPUtils.put(RegisterActivityThree.this, null, SPContants.MOBILE, phone);
						// SPUtils.put(App.ctx, null, SPContants.PASSWORD,
						// passwordedit.getText().toString());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.IS_LOGINED, true);

						login();
						
						RegisterActivityThree.this.finish();

						// 发送广播通知LoginActivity结束
						Intent intent1 = new Intent(LoginActivity.LOGINEXIT);
						sendBroadcast(intent1);
						LoginController.setLoginState(new LoginedState());
						
						ByAlert.alert("注册成功");
						
					}
				}if(entity.getEventType() == UserPresenter.register_fail){
					ByAlert.alert("注册失败");
				}
				if (entity.getEventType() == UserPresenter.user_success) {
					PersonDataBean bean = (PersonDataBean) entity.getData();
					if (bean != null) {

						SPUtils.put(RegisterActivityThree.this, null, SPContants.WX_Openid,
								bean.getWxopenid());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.USERNAME,
								bean.getUsername());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.MOBILE,
								bean.getMobile());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.IS_LOGINED, true);
						SPUtils.put(RegisterActivityThree.this, null, SPContants.EMAIL,
								bean.getEmail());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.NICK,
								bean.getNickname());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.PASSWORD,
								bean.getPassword());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.USER_ID,
								bean.getUid());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.AVATAR_URL,
								bean.getHead());
						Intent intent1 = new Intent(
								ActivitySetting.EXITCHANGE);
						sendBroadcast(intent1);
					}
				}
			}
		}
		
	}
	/**
	 * 登录
	 */
	public void login() {
		userPresenter = new UserPresenter(this);
		userPresenter.login(phone, res_three_num.getText().toString());
	}
	
	/*private void UserRegister() {//注册
		if (res_three_num.getText().toString().trim() == null) {
			T.showLong(RegisterActivityThree.this, "请输入验证码");
			return;
		}
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100").typeKey("BY_User_register")
				.param("from", "0")
				.param("reg_type", "3")
				.param("username", phone)
				.param("password", res_three_num.getText().toString())
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.USER_ID, response);
						SPUtils.put(RegisterActivityThree.this, null, SPContants.USERNAME,phone);
								
						SPUtils.put(RegisterActivityThree.this, null, SPContants.MOBILE, phone);
						// SPUtils.put(App.ctx, null, SPContants.PASSWORD,
						// passwordedit.getText().toString());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.IS_LOGINED, true);

						login();
						
						RegisterActivityThree.this.finish();

						// 发送广播通知LoginActivity结束
						Intent intent1 = new Intent(LoginActivity.LOGINEXIT);
						sendBroadcast(intent1);
						LoginController.setLoginState(new LoginedState());

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							T.showLong(RegisterActivityThree.this, msg);
						} else {
							T.showLong(RegisterActivityThree.this, exception.toString());
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}*/
	
/*	public void login() {//登录
		MyJsonRequest<PersonDataBean> request = new MyJsonRequest.Builder<PersonDataBean>()
				.apiVer("100")
				.typeKey("BY_User_login")
				.param("username",phone)
				.param("password",res_three_num.getText().toString())
				.requestListener(new XRequestListener<PersonDataBean>() {

					@Override
					public void onResponse(PersonDataBean response) {
						DebugLog.v("result", response.toString());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.WX_Openid,
								response.getWxopenid());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.USERNAME,
								response.getUsername());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.MOBILE,
								response.getMobile());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.IS_LOGINED, true);
						SPUtils.put(RegisterActivityThree.this, null, SPContants.EMAIL,
								response.getEmail());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.NICK,
								response.getNickname());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.PASSWORD,
								response.getPassword());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.USER_ID,
								response.getUid());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.AVATAR_URL,
								response.getHead());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.AQ_NUM,
								response.getAqCount());
						Intent intent1 = new Intent(
								ActivitySetting.EXITCHANGE);
						sendBroadcast(intent1);
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							T.showShort(RegisterActivityThree.this, msg);
						} else {
							T.showShort(RegisterActivityThree.this, exception.toString());
						}
					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}*/
}
